﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.DirectX;
using NGE;

namespace MH.MENU
{
    public class CHRFight:Panelbox
    {
        public CHRFight(Game g)
            : base(g)
        {
            Name = "人物战斗";
            Location = new Vector3(230, 130, 0);
            Size = new System.Drawing.Size(80, 100);

            gui_move = new Buttonbox();
            gui_move.Textbox.Text = "移动";
            gui_move.Location = new Vector3(0, 0, 0);
            gui_move.OnButtonboxEvent += new MenuEvent(gui_move_OnButtonboxEvent);

            gui_attack = new Buttonbox();
            gui_attack.Textbox.Text = "攻击";
            gui_attack.Location = new Vector3(0, 20, 0);
            gui_attack.OnButtonboxEvent += new MenuEvent(gui_attack_OnButtonboxEvent);

            gui_item = new Buttonbox();
            gui_item.Textbox.Text = "道具";
            gui_item.Location = new Vector3(0, 40, 0);
            gui_item.OnButtonboxEvent += new MenuEvent(gui_item_OnButtonboxEvent);

            gui_drive = new Buttonbox();
            gui_drive.Textbox.Text = "乘降";
            gui_drive.Location = new Vector3(0, 60, 0);
            gui_drive.OnButtonboxEvent += new MenuEvent(gui_drive_OnButtonboxEvent);

            gui_done = new Buttonbox();
            gui_done.Textbox.Text = "待机";
            gui_done.Location = new Vector3(0, 80, 0);
            gui_done.OnButtonboxEvent += new MenuEvent(gui_done_OnButtonboxEvent);

            AddElement(gui_move);
            AddElement(gui_attack);
            AddElement(gui_item);
            AddElement(gui_drive);
            AddElement(gui_done);

            OnPanelboxOpen += new MenuEvent(FightCHR_OnPanelboxOpen);
            OnPanelboxClose += new MenuEvent(FightCHR_OnPanelboxClose);
        }

        void FightCHR_OnPanelboxClose(object sender, object arg)
        {
            
        }

        void FightCHR_OnPanelboxOpen(object sender, object arg)
        {
            if (arg is string)
            {
                chr = sceneCOM.GetCHR((string)arg);
                isMoved = false;
                isAttacked = false;
            }
        }

        void gui_done_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                chr.AvatarAct = CharacterAvaterActs.Stand;
                scriptCOM.AS("menu close");
                //scriptCOM.AS(chr.Name + " check");
                scriptCOM.AS("battle notbusy");
            }
        }

        void gui_drive_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {

            }
        }

        void gui_item_OnButtonboxEvent(object sender, object arg)
        {
            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
            }
        }

        void gui_attack_OnButtonboxEvent(object sender, object arg)
        {
            if (isAttacked)
            {
                return;
            }

            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                //chr.AvaterAct = CharacterAvaterActs.Ready;

                //chr.BattleAct = CharacterActs.BattleAttackCommanding;
                chr.TargetLocation = chr.Location;
                isAttacking = true;
                isMoved = true;

                scriptCOM.AS(chr.Name + " avatar ready");
            }
        }

        void gui_move_OnButtonboxEvent(object sender, object arg)
        {
            if (isMoved)
            {
                return;
            }

            ButtonboxState bs = (ButtonboxState)arg;
            if (bs == ButtonboxState.Up)
            {
                //chr.BattleAct = CharacterActs.BattleMoveCommanding;
                chr.TargetLocation = chr.Location;
                isMoving = true;

                moveInfoHistory = new List<Datagram.PlayerCharacterMoveInfo>();

            }
        }
        protected override void OnKeyboardEventCode(object sender, int key, KeyboardState ks)
        {
            if (isMoving)
            {
                if (ks == KeyboardState.KeyUp)
                {
                    if (key == game.Option.KeyU || key == game.Option.KeyL || key == game.Option.KeyD || key == game.Option.KeyR)
                    {
                        Direction dir = Direction.U;

                        if (key == game.Option.KeyU)
                        {
                            dir = Direction.U;
                            chr.TargetLocation.Y -= sceneCOM.SCN.TileSize.Height;
                        }
                        else if (key == game.Option.KeyL)
                        {
                            dir = Direction.L;
                            chr.TargetLocation.X -= sceneCOM.SCN.TileSize.Width;
                            //chr.AvatarFlip = false;
                        }
                        else if (key == game.Option.KeyD)
                        {
                            dir = Direction.D;
                            chr.TargetLocation.Y += sceneCOM.SCN.TileSize.Height;
                        }
                        else if (key == game.Option.KeyR)
                        {
                            dir = Direction.R;
                            chr.TargetLocation.X += sceneCOM.SCN.TileSize.Width;
                            //chr.AvatarFlip = true;
                        }

                        //if (game.IsOnline)
                        //{
                        //    game.ClientCOM.UploadPCBattleInfo(sceneCOM.PC.BattleInfo);
                        //}

                        //if (sceneCOM.PC.BattleAct == PlayerCharacterActs.BattleMoveCommanding)

                        if (sceneCOM.PC.IsInCanMoveZone(chr.TargetLocation, sceneCOM.SCN))
                        {
                            Datagram.PlayerCharacterMoveInfo pcmi;
                            pcmi.Direction = dir;
                            pcmi.Steps = 1;
                            pcmi.Name = sceneCOM.PC.Name;
                            pcmi.MoveSpeed = sceneCOM.PC.MoveSpeed;

                            moveInfoHistory.Add(pcmi);
                        }
                        else
                        {
                            if (key == game.Option.KeyU)
                            {
                                chr.TargetLocation.Y += sceneCOM.SCN.TileSize.Height;
                            }
                            else if (key == game.Option.KeyL)
                            {
                                chr.TargetLocation.X += sceneCOM.SCN.TileSize.Width;
                            }
                            else if (key == game.Option.KeyD)
                            {
                                chr.TargetLocation.Y -= sceneCOM.SCN.TileSize.Height;
                            }
                            else if (key == game.Option.KeyR)
                            {
                                chr.TargetLocation.X -= sceneCOM.SCN.TileSize.Width;
                            }
                        }
                    }
                    else if (key == game.Option.KeyA)
                    {
                        sendCMD();
                        //chr.BattleAct = CharacterActs.Nothing;
                        isMoving = false;
                        isMoved = true;

                        scriptCOM.AS("menu close");
                        scriptCOM.AS("block " + chr.Name + " nothing");
                        scriptCOM.AS("menu open " + Name);
                    }
                    else if (key == game.Option.KeyB)
                    {
                        //chr.BattleAct = CharacterActs.Nothing;
                        isMoving = false;
                        isMoved = true;
                    }
                }
            }
            else if (isAttacking)
            {
                if (ks == KeyboardState.KeyUp)
                {
                    if (key == game.Option.KeyU || key == game.Option.KeyL || key == game.Option.KeyD || key == game.Option.KeyR)
                    {
                        //Direction dir;

                        if (key == game.Option.KeyU)
                        {
                            chr.Direction = Direction.U;
                            chr.TargetLocation.Y -= sceneCOM.SCN.TileSize.Height;
                        }
                        else if (key == game.Option.KeyL)
                        {
                            chr.Direction = Direction.L;
                            chr.TargetLocation.X -= sceneCOM.SCN.TileSize.Width;
                            if (chr.TargetLocation.X < chr.Location.X)
                            {
                                chr.AvatarFlip = false;
                            }
                        }
                        else if (key == game.Option.KeyD)
                        {
                            chr.Direction = Direction.D;
                            chr.TargetLocation.Y += sceneCOM.SCN.TileSize.Height;
                        }
                        else if (key == game.Option.KeyR)
                        {
                            chr.Direction = Direction.R;
                            chr.TargetLocation.X += sceneCOM.SCN.TileSize.Width;
                            if (chr.TargetLocation.X > chr.Location.X)
                            {
                                chr.AvatarFlip = true;
                            }
                        } 
                    }
                    else if (key == game.Option.KeyA)
                    {
                        if (chr.IsInCanAttackZone(chr.TargetLocation, sceneCOM.SCN))
                        {
                            Character tchr = sceneCOM.GetCHR(chr.TargetLocation);
                            if (tchr != null)
                            {
                                chr.AvatarAct = CharacterAvaterActs.Fight;
                                //chr.BattleAct = CharacterActs.Nothing;
                                isAttacking = false;
                                isAttacked = true;

                                scriptCOM.AS("menu close");

                                scriptCOM.AS("clrctrl");
                                scriptCOM.AS(chr.Name + " attack " + tchr.Name);
                                
                                scriptCOM.AS("block " + chr.Name + " nothing");
                                scriptCOM.AS("setctrl");
                                scriptCOM.AS("menu open " + Name);
                            }
                        }
                    }
                    else if (key == game.Option.KeyB)
                    {
                        chr.AvatarAct = CharacterAvaterActs.Stand;
                        //chr.BattleAct = CharacterActs.Nothing;
                        isAttacking = false;
                        isAttacked = true;
                    }
                }
            }
            else
            {               
                if (key == game.Option.KeyB)
                {
                    //chr.AvaterAct = CharacterAvaterActs.Stand;
                    //scriptCOM.AS("menu close");
                    //scriptCOM.AS("battle notbusy");
                    //isAttacking = false;
                    //isAttacked = false;
                    return;
                }                 
                base.OnKeyboardEventCode(sender, key, ks);
            }
            
        }

        bool isMoving;
        bool isMoved;
        bool isAttacking;
        bool isAttacked;
        //Vector3 offset;

        Character chr;

        List<Datagram.PlayerCharacterMoveInfo> moveInfoHistory;
        void sendCMD()
        {
            //List<Datagram.PlayerCharacterMoveInfo> bak = new List<Datagram.PlayerCharacterMoveInfo>();
            //for (int i = 1; i < moveInfoHistory.Count; i++)
            //{
            //    if (moveInfoHistory[i - 1].Direction != Util.GetOppositeDirection(moveInfoHistory[i].Direction))
            //    {
            //        bak.Add(moveInfoHistory[i - 1]);
            //        if (i + 1 == moveInfoHistory.Count)
            //        {
            //            bak.Add(moveInfoHistory[i]);
            //        }
            //    }
            //}
            //moveInfoHistory = new List<Datagram.PlayerCharacterMoveInfo>();
            foreach (Datagram.PlayerCharacterMoveInfo pcmi in moveInfoHistory)
            {
                chr.Move(pcmi);
            }
        }

        Buttonbox gui_move;
        Buttonbox gui_attack;
        Buttonbox gui_item;
        Buttonbox gui_drive;
        Buttonbox gui_done;

        SceneComponent sceneCOM
        {
            get
            {
                return ((SceneComponent)game.GetComponent("SceneCOM"));
            }
        }
        ScriptComponent scriptCOM
        {
            get
            {
                return ((ScriptComponent)game.GetComponent("ScriptCOM"));
            }
        }
    }
}
